{% extends "../../layui/layouts/main.html" %}

{% block mainbody %}
<div id="menu-index">

    <script type="text/html" id="toolBar">
        <a class="layui-btn layui-btn-normal layui-btn-sm" lay-event="create">
            <i class="layui-icon layui-icon-add-circle"> 创建管理员</i>
        </a>
    </script>

    <script type="text/html" id="tool">
        <a class="layui-btn layui-btn-success layui-btn-xs" lay-event="reset-pwd">重置密码</a>
        <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
    </script>

    <div class="layui-card">
        <div class="layui-card-header  yak-card-header">
            {{Header}}
        </div>
        <div class="layui-card-body">
            <table id="sys-menus-table" lay-filter="sys-menus-table-filter"></table>
        </div>
    </div>

</div>
{% endblock %}

{% block script %}
<script>
    layui.use(['grid'], function () {
        var pager = layui.grid
            , tableId = "sys-menus-table"
            , tableFilter = "sys-menus-table-filter"
            , searchFilter = "sys-menus-search"
            , cols = [
            {width: 60, field: "_serial", title: "序号" },
            {field: "username", title: "管理员账号"},
            {field: "password", title: "管理员密码"},
            {field: "nickname", title: "管理员昵称"},
            {field: "address", title: "所属地区"},
            {field: "last_login_time", title: "上次登录时间"},
            {fixed: 'right', width:150, align:'center', toolbar: '#tool'}
        ]
            , table = {
            "id": tableId,
            "elem": "#" + tableId,
            "limit": 8,
            "limits": [10, 20, 50, 100],
            "cols": [cols],
            "method": "post",
            "request": {"pageName": "page", "limitName": "per-page"},
            "url": "/admin/list",
            "toolbar": "#toolBar",
            "page": {"hash": "fenye"},
            "autoSort": false,
            "defaultToolbar": []
        }
            , eventOptions = {
            "create": {"url": "/admin/form", "openRefresh": true},
            "reset-pwd": {"url": "/admin/reset-pwd", method: "post", "openRefresh": true, message: "是否将密码重置为原始密码？"},
            "del": {url: "/admin/del", method: "delete", message: "确定删除该用户？"},
            "OUTPUT_EXCEL": {"url": "/admin/output-excel", "filename": "管理员列表"},
        }
            , events = {
            "reset-pwd": function (obj, config, that) {
                var helper = layui.helper
                if (!helper.hasKey(config, 'url')) {
                    layer.alert('请先配置reset password请求地址');
                    return false
                }

                var $ax = layui.ax
                    , condition = helper.getValue(config, 'condition', {id: ':id'})
                    , message = helper.getValue(config, 'message', '')
                    , method = helper.getValue(config, 'method', 'get')
                    , params = helper.getCondition(condition, obj.data)

                layer.alert(message, function (index) {
                    var ajax = new $ax(config.url + '?' + helper.urlEncode(params));
                    ajax.setMethod(method);
                    ajax.setResult(function (result) {
                        that.reloadTable(null, null, true);
                        layer.close(index);
                        layer.msg(result.msg)
                    });
                    ajax.send();
                })
            }
        }

        pager.render({
            "searchFilter": searchFilter,
            "tableId": tableId,
            "tableFilter": tableFilter,
            "sort": "server",
            "countCache": true,
            "table": table,
            "editor": {
                "method": "get",
                "url": "/sys/menu/update",
                "select": [{"field": "pid", "data": [{"name": "1", "value": "TTT"}]}]
            },
            "eventOptions": eventOptions
        }, events)
    })
</script>
{% endblock %}